import sys,math,os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class DatetimeDemo(QWidget):
    def __init__(self):
        super(DatetimeDemo,self).__init__()
        self.initUI()
    def initUI(self):
        self.setWindowTitle("QdateTime案例")
        self.resize(300,100)
        vlayout=QVBoxLayout()
        self.datetimeEdit=QDateTimeEdit(QDateTime.currentDateTime(),self)
        self.datetimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")

        self.datetimeEdit.setMinimumDate(QDate.currentDate().addDays(-365))

        self.datetimeEdit.setMaximumDate(QDate.currentDate().addDays(365))
        self.datetimeEdit.setCalendarPopup(True)
        self.datetimeEdit.dateChanged.connect(self.onDateChanged)
        self.datetimeEdit.dateTimeChanged.connect(self.onDateTimeChanged)
        self.datetimeEdit.timeChanged.connect(self.onTimeChanged)
        self.btn=QPushButton("获得日期和时间")
        self.btn.clicked.connect(self.onButtonClick)
        vlayout.addWidget(self.datetimeEdit)
        vlayout.addWidget(self.btn)
        self.setLayout(vlayout)
    def onDateChanged(self,date):
        print(date)
    def onDateTimeChanged(self,dateTime):
        print(dateTime)
    def onTimeChanged(self,time):
        print(time)

    def onButtonClick(self):
        dateTime=self.datetimeEdit.dateTime()
        print(str(dateTime))
        print(str(self.datetimeEdit.maximumDate()))


if __name__ == '__main__':
    app=QApplication(sys.argv)
    demo=DatetimeDemo()
    demo.show()
    sys.exit(app.exec_())
